<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	$this->view->headTitle("Demo Module Book");
    }

    public function indexAction()
    {
        // action body
        $books = new Application_Model_DbTable_Books();
        $all = $books->listBooks();
		
        //$pager = new Zend_Paginator();
        //$pager->factory($all);
        
        $pager = Zend_Paginator::factory($all);
        $pager->setItemCountPerPage(3);
        $pager->setPageRange(3);
        $currentpage = $this->_request->getParam('page',1);
        $pager->setCurrentPageNumber($currentpage);
		
        $this->view->books = $pager;
        $this->view->message = $this->_helper->alert->show();
    }
    
    public function addAction()
    {
    	// action body
    	$form = new Application_Form_AddBook();
    	$this->view->form = $form;
    	
    	//if POST
	    if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$data = new Application_Model_Book($form->getValues());

				$books = new Application_Model_DbTable_Books();
				$books->addBook($data);
				
				$msg = 'Added new book successful';
				//show message use flashMessenger
	    		//$this->_helper->flashMessenger->addMessage($msg);
	    		//show message use my controller helper
	    		$this->_helper->alert($msg);
			} else {
				$form->populate($formData);
				
				$msg = "Book's information is not valid";
				//show message use flashMesseger
				//$this->_helper->flashMessenger->addMessage($msg);
				//show message use my controller helper
				$this->_helper->alert($msg);
				
			}
			
		}
		
		if($this->_helper->flashMessenger->hasMessages()){
			$this->view->messages = $this->_helper->flashMessenger->getMessages();
		}
		//show on view
		$this->view->message = $this->_helper->alert->show();
	}
	
	public function editAction(){
		$form = new Application_Form_EditBook();
		$this->view->form = $form;
		if ($this->getRequest()->isPost()) {
			$formdata = $this->getRequest()->getPost();
			if($form->isValid($formdata)){
				$id = $form->getValue('id');

				$data = new Application_Model_Book($form->getValues());
				$book = new Application_Model_DbTable_Books();
				if($data->image !== null){
					$book->saveBook($data, $id);
				}else{
					$book->saveBookNoImg($data, $id);
				}
				
				//show message use my controller helper
				$this->_helper->alert('Edited book successful');
				//on indexAction set show message so we don't need code in here
				$this->_helper->redirector('index');
			}else{
				$form->populate($formdata);
				$msg = "Book's information is not valid";
				//show message use flashMesseger
				//$this->_helper->flashMessenger->addMessage($msg);
				//show message use my controller helper
				$this->_helper->alert($msg);
				
				//if($this->_helper->flashMessenger->hasMessages()){
				//	$this->view->messages = $this->_helper->flashMessenger->getMessages();
				//}
				//show on view
				$this->view->message = $this->_helper->alert->show();
			}
		}
		//nếu là GET
		else{
			$id = $this->_getParam('id');
			if($id > 0 ){
				$book = new Application_Model_DbTable_Books();
				$data = $book->getBook($id,'array');
				if(!empty($data)){
					$form->imageex->setAttrib('src',Zend_Controller_Front::getInstance()->getBaseUrl().'/public/upload/'.$data["image"]);
					$form->populate($data);
				}else{
					$this->_helper->alert('No book found');
					$this->_helper->redirector('index');
				}
			}else{
				$this->_helper->alert('No book found');
				$this->_helper->redirector('index');
			}
		}
	}
	
	public function deleteAction(){
		$this->getHelper('viewRenderer')->setNoRender();
		if(!$this->getRequest()->isPost()){
			$id = $this->_getParam('id');
			if($id > 0){
				$book = new Application_Model_DbTable_Books();
				$book->deleteBook($id);
			
				$this->_helper->alert('Deleted');
			}
		}
		$this->_redirect($_SERVER['HTTP_REFERER']);
		//$this->_helper->redirector('index');
	}

}

